capt log close _all
log using   "${logs}FigureA4" , replace text
*------------

// Time to run: X minutes

*==================================================================
* Figure A.4: Residualized road tolls and bus lance distances, by year. 2008-2017
*==================================================================

use    "${dataout}MainDataset" , clear

keep if couple==1
keep if year>=2008 & year<=2017

// Shorten variable name
rename  ptl_fam_km_mean ptl_fam_mean

label variable toll_fam_mean "Road toll (NOK)" 
label variable ptl_fam_mean "Bus lane (km)"

program cefhist
syntax varlist , xrange(string) yrange(string) ygrid(real) c(real) xlab(string) [addgraph(string)] [at(string)] *
tokenize `varlist'
local yvar `1'
local xvar `2'
local lineoptions lpattern(solid) lcolor(red)
quietly {
	tempvar x xnew ynew
	g `xnew' = .
	g `ynew' = .
	g `x'    = round(`yvar', `ygrid')
	bysort `x' : replace `x' = . if _n>1

	levelsof `xvar'
	if ("`at'"=="") local at `r(levels)'
	foreach e of num `r(levels)' {
		sum `yvar' if `xvar'==`e'
		replace `ynew' = r(mean) in `e'
		replace `xnew' = `e' in `e'
	}
	
	tempvar r y
	g `y'    = round(`yvar', `ygrid')
	bys `xvar' `y': g `r' = _N 
	bys `xvar': replace `r' = `r'/_N
	bys `xvar' `y': replace `r' = . if _n>1
	foreach e of num `at' {
		tempvar x`e' y`e' y`e'_0
		g `x`e'' = `y' if `xvar'==`e'
		g `y`e'' = `r' if `xvar'==`e'
		sum `y`e''
		replace `y`e'' = `e' + `y`e''/(r(max)*`c')
		g `y`e'_0' = `e'
		local gline `gline' (rbar `y`e'' `y`e'_0' `x`e'' if inrange(`x`e'',`yrange'), barwidth(`ygrid') horizontal lcol(black) lwidth(thin) fcol(ltbluishgray))
	}

	noi twoway `gline' (con `ynew' `xnew' if inrange(`xvar',`xrange'), sort ms(O) lpat(shortdash) mcol(red) lwidth(medthick)) `addgraph', ///
		legend(off) plotregion(margin(zero))  graphregion(color(white))  xlab(`xlab') `options'

	*gr export cefhist_`yvar'_`xvar'.eps, replace
}
end

*============================================================================
// Absorb 3 way FE from variables
// Year spesific
// 2008-2017
*============================================================================

local xvarlist  toll_fam_mean ptl_fam_mean  
local yearlist 08 09 10 11 12 13 14 15 16 17

foreach x in `xvarlist' {
foreach year in `yearlist' {
	dis "Working on year 20`year'"
	capt drop res`year'_`x'
	capt drop resM`year'_`x'
	reghdfe `x'  if year==20`year' , absorb($FE)  residuals(res`year'_`x') 
	sum `x'  if e(sample)==1
	return list
	dis "`r(mean)'"
	gen res`year'M_`x'=  res`year'_`x' + `r(mean)'
}
}

capt gen resYM_toll_fam_mean=.
capt gen resYM_ptl_fam_mean=.

local xvarlist  toll_fam_mean /*ptl_fam_mean  */
local yearlist 08 09 10 11 12 13 14 15 16 17

foreach x in `xvarlist' {
foreach year in `yearlist' {
	capt replace resYM_`x' =res`year'M_`x' if year==20`year'
}
}

// Restrict to 2008-2017
cefhist resYM_toll_fam_mean year , xrange(2008,2017) yrange(-20,40) ygrid(2) c(1) xlab(2008(1)2017) ytitle("Road toll (NOK)") scale(1.3)
graph export  "${figures}FigureA4a.png" , replace
graph export  "${figures}FigureA4a.pdf" , replace

cefhist resYM_ptl_fam_mean year , xrange(2008,2017) yrange(-1,2) ygrid(0.1) c(1) xlab(2008(1)2017) ytitle("Bus lane (km)") scale(1.3)
graph export  "${figures}FigureA4b.png" , replace
graph export  "${figures}FigureA4b.pdf" , replace

// Restrict to 2015-2017
keep if year>=2015 & year<=2017

cefhist resYM_toll_fam_mean year , xrange(2015,2017) yrange(-20,40) ygrid(2) c(1) xlab(2015(1)2017) ytitle("Road toll (NOK)") scale(1.3)
graph export  "${figures}FigureA4c.png" , replace
graph export  "${figures}FigureA4c.pdf" , replace

cefhist resYM_ptl_fam_mean year , xrange(2015,2017) yrange(-1,2) ygrid(0.1) c(1) xlab(2015(1)2017) ytitle("Bus lane (km)") scale(1.3)
graph export  "${figures}FigureA4d.png" , replace
graph export  "${figures}FigureA4d.pdf" , replace

*------------
log close
